//
//  system_MessageViewController.m
//  SoundPill
//
//  Created by 宋旭东 on 16/4/20.
//  Copyright © 2016年 宋旭东. All rights reserved.
//

#import "system_MessageViewController.h"
#import "chatViewController.h"
#import "userInformationViewController.h"
#import "playerTableViewController.h"
#import "UIViewController+MMDrawerController.h"
#import "message_SystemView.h"
#import "EMSDK.h"
#import "EMConversation.h"
#import "WZLBadgeImport.h"
#import "UIViewExt.h"
@interface system_MessageViewController ()<UIScrollViewDelegate>
{
    float offset;
}
@property (strong, nonatomic) UIButton *systemBtn;
@property (strong, nonatomic) UIButton *messageBtn;
@property (strong, nonatomic) UIScrollView *bigScroll;
@property (strong, nonatomic) message_SystemView *SystemView;
@end

@implementation system_MessageViewController
- (instancetype)init
{
    self = [super init];
    if (self) {
//        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"系统消息";
    
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    [back setFrame:CGRectMake(0, 0, 30, 30)];
    [back setImage:[UIImage imageNamed:@"backButton"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(backTo) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:back];
    self.navigationItem.leftBarButtonItem = backItem;
    
    [self createSystemMessageView];
}
- (void)createSystemMessageView
{
    __weak typeof(self) weakself = self;
    //系统
    self.SystemView = [[message_SystemView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
    _SystemView.tableview.scrollsToTop = YES;
    [self.view addSubview:_SystemView];
    _SystemView.block = ^(NSString *action, NSString *userid, NSString *workid) {
        if ([action isEqualToString:@"people"]) {
//            跳转用户
            [weakself pushuserControllerWithid:userid];
        } else {
//            跳转作品
            [weakself pushPlayerControllerWithid:workid];
        }
    };
}
- (void)backTo
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)pushuserControllerWithid:(NSString *)userid
{
    if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"uid"] isEqualToString:userid]) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    } else {
        userInformationViewController *userinformation = [[userInformationViewController alloc] initWithSelfUid:userid];
        [self.navigationController pushViewController:userinformation animated:YES];
    }
}
- (void)pushPlayerControllerWithid:(NSString *)workid
{
    UINavigationController *nv = (UINavigationController *)self.mm_drawerController.rightDrawerViewController;
    playerTableViewController *player = (playerTableViewController *)nv.topViewController;
    Player_ShowModel *playModel = [Player_ShowModel sharedInstance];
    NSString *showURL = [NSString stringWithFormat:@"http://123.57.1.143/api/work/%@/show", workid];
     AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    NSString *header = [NSString stringWithFormat:@"Token %@:%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"uid"], [[NSUserDefaults standardUserDefaults] valueForKey:@"token"]];
    [manager.requestSerializer setValue:header forHTTPHeaderField:@"authorization"];
    [manager GET:showURL parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = (NSDictionary *)responseObject;
        NSDictionary *dataDic = [dic valueForKey:@"data"];
        NSDictionary *linksDic = [dic valueForKey:@"links"];
        [playModel setValuesForKeysWithDictionary:dataDic];
        if ([[dic allKeys] containsObject:@"meta"]) {
            NSDictionary *meta = [dic valueForKey:@"meta"];
            NSString *userAvatar = [meta valueForKey:@"userAvatar"];
            player.locUserAvatar = userAvatar;
//            NSArray *array = [meta valueForKey:@"playlist"];
//            player.playList = [NSMutableArray arrayWithArray:array];
            playModel.comment_count = [meta valueForKey:@"count"];
            ;
        }
        player.waveform = [linksDic valueForKey:@"waveform"];
        player.workID = workid;
        player.isSelf = YES;
        //保存是否love
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        NSString *islove = @"0";
        if ([[dataDic objectForKey:@"liked"]boolValue]==YES) {
            islove = @"1";
        }else{
            islove = @"0";
        }
        [userDefaults setObject:islove forKey:@"isLove"];
        [userDefaults synchronize];
        [player reloadViews:playModel];
        [self.mm_drawerController toggleDrawerSide:MMDrawerSideRight animated:YES completion:^(BOOL finished) {
        }];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
@end
